<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <link rel="shortcut icon" href="client/dist/walkoff/favicon.ico" type="image/x-icon">
    <link rel="icon" href="client/dist/walkoff/favicon.ico" type="image/x-icon">
    <link rel="stylesheet" href="client/resources/bootstrap/dist/css/bootstrap.min.css">
    <style>
        body {
            font-family: "Verdana", sans-serif;
        }

        #logo {
            height: 300px;
            width: 400px;
            margin: 0 auto;
            display: block;
        }

        h2 {
            text-align: right;
        }

        .row {
            margin-bottom: 10px;
        }

        div.loginFormContainer {
            width: 640px;
            margin: 0 auto 20px auto;
            border-radius: 5px;
            background-color: #F4F8FF;
            padding: 10px;
            box-shadow: 0px 2px 5px #CCC;
        }

        div.loginFormContainer label {
            font-size: 1.4em;
            margin-top: 3px;
            font-weight: 100;
        }

        div.loginFormContainer #message {
            font-size: 1.4em;
            margin-top: 3px;
            color: crimson;
        }

        .btn-primary {
            color: #fff;
            background-color: #337ab7;
            border-color: #337ab7;
        }

        .btn-primary:hover {
            color: #fff;
            background-color: #2b6699;
            border-color: #285f8f;
        }
    </style>
</head>

<body>
    <div class="form">
        <div class="formHeader">
            <img id="logo" src="client/dist/walkoff/assets/img/walkoffLogo.png"/>
        </div>

        <div class="loginFormContainer">
            <h2><strong>WALK</strong>OFF Login</h2>
            <form id="loginForm" action="/login-process" method="POST">
                <div class="row">
                    <div class="col-3">
                        <label for="name">Username</label>
                    </div>
                    <div class="col-9">
                        <input id="username" type="text" class="form-control" />
                    </div>
                </div>
                <div class="row">
                    <div class="col-3">
                        <label for="name">Password</label>
                    </div>
                    <div class="col-9">
                        <input id="password" type="password" class="form-control" />
                    </div>
                </div>
                <div class="row">
                    <div class="col">
                        <span id="message"></span>
                    </div>
                    <div class="col-auto">
                        <input type="submit" id="login" class="btn btn-primary pull-right" value="Login" />
                    </div>
                </div>
            </form>
        </div>
        <span class="clear"></span>
    </div>

    <script src="client/resources/jquery/dist/jquery.min.js"></script>
    <script src="client/resources/bootstrap/dist/js/bootstrap.min.js"></script>
    <script>
        $("#loginForm").on("submit", function (e) {
            e.preventDefault();
            var username = $("#username").val();
            var password = $("#password").val();
            $.ajax({
                url: '/walkoff/api/auth/login',
                contentType: 'application/json',
                data: JSON.stringify({ "username": username, "password": password }),
                type: "POST",
                async: false,
                success: function (data) {
                    sessionStorage.setItem("access_token", data["access_token"]);
                    sessionStorage.setItem("refresh_token", data["refresh_token"]);
                    setCookie("access_token", data["access_token"]);
                    setCookie("refresh_token", data["refresh_token"]);
                    location.href = "/walkoff";
                },
                error: function (e) {
                    $("#message").text("Invalid Login");
                }
            });
        });

        function setCookie(cname, cvalue) {
            document.cookie = cname + "=" + cvalue + ";path=/";
        }

    </script>
</body>

</html>
